package com.edgclub.edg.activity;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.edgclub.edg.R;
import com.edgclub.edg.utils.util.ActivityManager;
import com.hyphenate.easeui.controller.EaseUI;
import com.umeng.analytics.MobclickAgent;

/**
 * 底层Activity
 */
public abstract class GzBaseActivity extends FragmentActivity implements
        OnCancelListener {
    protected InputMethodManager inputMethodManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //http://stackoverflow.com/questions/4341600/how-to-prevent-multiple-instances-of-an-activity-when-it-is-launched-with-differ/
        //理论上应该放在launcher activity,放在基类中所有集成此库的app都可以避免此问题
        if (!isTaskRoot()) {
            Intent intent = getIntent();
            String action = intent.getAction();
            if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && action.equals(Intent.ACTION_MAIN)) {
                finish();
                return;
            }
        }

        inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//        ActivityManager.getInstance().addActivity(this);
//        initSystemBar(this);
    }

    public void initSystemBar(Activity activity) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            setTranslucentStatus(activity, true);
        }

    }

    @TargetApi(19)

    private void setTranslucentStatus(Activity activity, boolean on) {

        Window win = activity.getWindow();

        WindowManager.LayoutParams winParams = win.getAttributes();

        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;

        if (on) {

            winParams.flags |= bits;

        } else {

            winParams.flags &= ~bits;

        }

        win.setAttributes(winParams);

    }

    @Override
    protected void onDestroy() {
        try {
            super.onDestroy();
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }
            ActivityManager.getInstance().removeActivity(this);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }

    }

    @Override
    public void onStop() {
        super.onStop();
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }

    }

    android.app.Dialog dialog;
    private Animation anim;
    /**
     * 显示dialog
     *
     * @param message
     */
    private ImageView load_image;

    public void showDialog(String message, boolean isCanCancel) {
        try {
            anim = AnimationUtils.loadAnimation(this, R.anim.my_anim);
            LinearInterpolator lir = new LinearInterpolator();
            anim.setInterpolator(lir);
            dialog = new android.app.Dialog(GzBaseActivity.this, R.style.dialog);
            if (!dialog.isShowing() && !this.isFinishing()) {
                dialog.show();
            }
            Window window = dialog.getWindow();
            window.setContentView(R.layout.showdialog_layout);
            load_image = (ImageView) window.findViewById(R.id.load_image1);
            TextView load_text = (TextView) window.findViewById(R.id.load_text);
            load_text.setText(message + "");
            dialog.setCanceledOnTouchOutside(isCanCancel);
            load_image.startAnimation(anim);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 取消dialog
     */
    public void dismissDialog() {
        try {
            if (null != anim)
                anim.cancel();
            if (dialog != null) {
                dialog.dismiss();
                dialog = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void dialogCancel(DialogInterface dialog) {
        if (null != anim)
            anim.cancel();
        if (null != dialog) {
            dialog.dismiss();
        }
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        if (null != anim)
            anim.cancel();
        dialogCancel(dialog);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // onresume时，取消notification显示
        // onresume时，取消notification显示
        MobclickAgent.onResume(this);
        try {
            EaseUI.getInstance().getNotifier().reset();
        } catch (Exception e) {

        }

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        // onPause时，暂停友盟统计
        MobclickAgent.onPause(this);
    }

    // // 重写此方法设置需要填充的布局
    // protected abstract int getLayout();
    //
    // protected abstract void initView();

    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    protected void hideSoftKeyboard() {
        if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (getCurrentFocus() != null)
                inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 返回
     *
     * @param view
     */
    public void back(View view) {
        finish();
    }
}
